<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\dao\car;


use app\dao\BaseDao;
use app\model\car\CarOccupyTime;

/**
 * Class CarOccupyTimeDao
 * @package app\dao\car
 */
class CarOccupyTimeDao extends BaseDao
{
    protected function setModel(): string
    {
        return CarOccupyTime::class;
    }

    /**
     * 列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public function getList(array $where = [],int $page,int $limit){
        $model = $this->getModel()->alias('ot')->where('ot.is_del','=',0);
        $model->join('car_info ci','ci.id = ot.car_id','left');
        $model->join('car_type ct','ct.id = ci.type_id','left');
        $model->join('base_place bp','bp.id = ct.place_id','left');
        $model = $this->setWheres($where,$model);
        $model->field('ot.id,ot.car_id,bp.title as shop_name,ci.car_no,ot.start_time,ot.end_time,ot.occupy_type');
        $count = $model->count();
        $list = $model->order('ot.id desc')->page($page,$limit)->select()->toArray();
        return compact('list','count');
    }

    /**
     *
     * 搜索条件
     * @param $where
     * @param $model
     * @return mixed
     */
    public function setWheres($where,$model){
        if(isset($where['keyword'])&&$where['keyword']){
            $model->where('ci.car_no','like',"%{$where['keyword']}%");
        }
        if(isset($where['place_id'])&&$where['place_id']){
            $model->where('ct.place_id',$where['place_id']);
        }
        return $model;
    }
}
